import { baseUrl } from "./env";
import { YkMessage } from "@yike-design/ui";
import axios from "axios";

const service = axios.create({
  baseURL: baseUrl, // api的base_url
  timeout: 5000, // 请求超时时间
});

//添加请求拦截器
service.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    YkMessage({type:'warning', message: error.message})
    return Promise.reject(error);
  }
);

//添加响应拦截器
service.interceptors.response.use(
  (response) => {
    if(response.status === 200){
        return response;
    }
    else{
        YkMessage({type:'warning', message: response.statusText})
        return Promise.reject(response.statusText);
    }
  },
  (error) => {
    YkMessage({type:'warning', message: error.message})
    return Promise.reject(error);
  }
);

export default service;